﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections.Generic;

public class VnSharing
{
    public const string SOURCE = "http://truyen.vnsharing.net/";
    public const int MANGAID = 1;
    public struct ChapterInfo
    {
        public string Link;
        public string Name;
    };
    
    /// <summary>
    /// Get chapter information (link & name) from html text
    /// </summary>
    /// <param name="result"></param>
    /// <param name="mangaName"></param>
    /// <returns>List of ChapterInfo</returns>
    public static List<ChapterInfo> GetChapterLinks(string result, string mangaName)
    {
        // get list of match have "href"
        //string pattern = @"<(a\s+href)\b[^>]*>\b[^>]*>"; // <a/space*n/href/string/>string> // chuan
        string pattern = @"\<(a\s+href)[^>]*/Truyen/[^>]*title[^>]*>[^>]*\>";
        // <a  href=\"/Truyen/Kami-no-mizo-Shiru-Sekai/Flag-149\" title=\"Đọc Kami nomi zo Shiru Sekai Flag. 149 - Chuyện về m&#236;nh online\">\r\n Kami nomi zo Shiru Sekai Flag. 149 - Chuyện về mình</a>

        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection matches = rgx.Matches(result);

        List<ChapterInfo> chapters = new List<ChapterInfo>();

        // for test
        for (int index = 0; index < matches.Count; index++)
        {
            Debug.WriteLine(matches[index].Value);
        }

        Debug.WriteLine("--------------------------------------");
        
        

        int NumberOfChapter = matches.Count;

        for (int index = 0, l = matches.Count; index < l; index++)
        {
            // Get match which has chapter link
            string search = @"(?<="")/Truyen/" + mangaName + @"/.+?(?="")"; // get content inside "content" // /Truyen/mangaName/string = content
            Regex regex = new Regex(search, RegexOptions.IgnoreCase);
            Match chapterLink = regex.Match(matches[index].Value);

            
            // insert match into List of ChapterInfo
            if (chapterLink.Value != "")
            {
                //Debug.WriteLine(chapterLink.Value);
                ChapterInfo chap = new ChapterInfo();
                chap.Link = chapterLink.Value;
                

                //string searchName = @"(?<=>).+?(?=<)";
                ////string searchName = @"\\r\\n.+?";
                //Regex rgxName = new Regex(searchName, RegexOptions.IgnoreCase);
                string value = matches[index].Value;
                //Debug.WriteLine(value.LastIndexOf("\r\n").ToString());
                //Debug.WriteLine(value.Substring(value.LastIndexOf("   ") + 3, value.LastIndexOf("<") - (value.LastIndexOf("   ") + 3)));
                //string test = value.Substring(value.LastIndexOf("\r\n"));
                
                //Match name = rgxName.Match(value);
                chap.Name = value.Substring(value.LastIndexOf("   ") + 3, value.LastIndexOf("<") - (value.LastIndexOf("   ") + 3));
                //if (name.Value.Length > 0)
                //{
                //    chap.Name = name.Value.Substring(5);
                //}
                //else
                //{
                //    chap.Name = chap.Link.Substring(chap.Link.LastIndexOf("/") + 1);
                //}
                //chap.Name = String.Format("Chapter {0}", NumberOfChapter.ToString("D3"));
                //NumberOfChapter--;
                chapters.Add(chap);
            }
        }

        // remove external countinued chapter
        chapters.RemoveAt(chapters.Count - 1);

        chapters.Reverse();
        // for test
        for (int index = 0; index < chapters.Count; index++)
        {
            Debug.WriteLine(chapters[index].Link + " - " + chapters[index].Name);
        }

        return chapters;
    }

    public static string GetImage(string result)
    {
        string pattern = @"<(img\s+width)\b[^>]*>";

        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        Match match = rgx.Match(result);

        string patternReturn = @"(?<="")http.+?(?="")";
        Regex rgxReturn = new Regex(patternReturn, RegexOptions.IgnoreCase);
        Match matchReturn = rgxReturn.Match(match.Value);

        return matchReturn.Value;
    }

}

